/**
 * 
 */
package com.acxiom.bsh.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acxiom.bsh.constants.CommonConstant;
import com.acxiom.bsh.entity.DrawDouble11Result;
import com.acxiom.bsh.exception.BshException;
import com.acxiom.bsh.pojo.Double11PrizeRecord;
import com.acxiom.bsh.service.LuckyDrawDouble11Service;

/**
 * @Description: 双十一红包抽奖活动
 * @author jerche
 *
 * @updateUser
 * @updateDate
 */
@Controller
@RequestMapping(value = "/double11")
public class LuckyDrawDouble11Controller {
	@Autowired
	private LuckyDrawDouble11Service double11Service;

	/**
	 * 红包抽奖
	 * 
	 * @Title: draw
	 * @param session
	 *            存储淘宝用户信息会话
	 * @return
	 */
	@RequestMapping(value = "/draw")
	@ResponseBody
	public DrawDouble11Result draw(HttpSession session) {
		String mixNick = getNickName(session);
		if (mixNick == null) {
			return null;
		}
		DrawDouble11Result result = null;
		try {
			result = double11Service.double11Draw(mixNick);
			return result;
		} catch (Exception e) {
			if (result == null) {
				result = new DrawDouble11Result();
			}
			if (StringUtils.isEmpty(result.getCode())) {
				result.setCode("099");
				result.setMessage("系统正忙，请稍后");
			}
		}
		return result;
	}

	/**
	 * 红包抽奖记录
	 * 
	 * @Title: drawList
	 * @param session
	 *            存储淘宝用户信息会话
	 * @return
	 */
	@RequestMapping(value = "/draw/list")
	@ResponseBody
	public List<Double11PrizeRecord> drawList(HttpSession session) throws BshException {
		List<Double11PrizeRecord> result = null;
		try {
			String mixNick = getNickName(session);
			if (mixNick == null) {
				return null;
			}
			result = double11Service.getDouble11PrizeList(mixNick);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 红包抽奖top5名单
	 * 
	 * @Title: getDouble11PrizeRecordTop5 抽奖top5名单
	 * @param session
	 *            存储淘宝用户信息会话
	 * @return
	 */
	@RequestMapping(value = "/draw/top5")
	@ResponseBody
	public List<Double11PrizeRecord> getDouble11PrizeRecordTop5(HttpSession session) throws BshException {
		List<Double11PrizeRecord> result = null;
		try {
			String mixNick = getNickName(session);
			if (mixNick == null) {
				return null;
			}
			result = double11Service.getDouble11PrizeTop5();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取用户nick信息
	 * 
	 * @Title: getNickName
	 * @param session 
	 *            存储淘宝用户信息会话
	 * @return
	 */
	private String getNickName(HttpSession session) {
		Object obj = session.getAttribute(CommonConstant.TAOBAO_NICK_ATTRIBUTE);
		String nickName = null;
		if (obj != null) {
			nickName = obj.toString();
		}
		return nickName;
	}

}
